package ian.android.lovewords.ui;

import ian.android.lovewords.R;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ActivityHelper {
	
	protected Activity mActivity;
	
	protected ActivityHelper(Activity activity) {
        mActivity = activity;
    }
	
	public static ActivityHelper createInstance(Activity activity) {
        return new ActivityHelper(activity);
    }
	
    public void goHome() {
        if (mActivity instanceof ActivityHome) {
            return;
        }

        final Intent intent = new Intent(mActivity, ActivityHome.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        mActivity.startActivity(intent);
    }
    
    public ViewGroup getActionBarCompat() {
        return (ViewGroup) mActivity.findViewById(R.id.actionbar_compat);
    }
    
    public View addActionButtonCompat(int iconResId, int textResId,
            View.OnClickListener clickListener, boolean separatorAfter) {
        final ViewGroup actionBar = getActionBarCompat();
        if (actionBar == null) {
            return null;
        }

        // Create the separator
        ImageView separator = new ImageView(mActivity, null, R.attr.actionbarCompatSeparatorStyle);
        separator.setLayoutParams(
                new ViewGroup.LayoutParams(2, ViewGroup.LayoutParams.FILL_PARENT));

        // Create the button
        ImageButton actionButton = new ImageButton(mActivity, null,
                R.attr.actionbarCompatButtonStyle);
        actionButton.setLayoutParams(new ViewGroup.LayoutParams(
                (int) mActivity.getResources().getDimension(R.dimen.actionbar_compat_height),
                ViewGroup.LayoutParams.FILL_PARENT));
        actionButton.setImageResource(iconResId);
        actionButton.setScaleType(ImageView.ScaleType.CENTER);
        actionButton.setContentDescription(mActivity.getResources().getString(textResId));
        actionButton.setOnClickListener(clickListener);

        // Add separator and button to the action bar in the desired order

        if (!separatorAfter) {
            actionBar.addView(separator);
        }

        actionBar.addView(actionButton);

        if (separatorAfter) {
            actionBar.addView(separator);
        }

        return actionButton;
    }
    
    public void addActionButtonCompatRight(int iconResId, int textResId,
            View.OnClickListener clickListener){
    	
    }
    
    /* Sets up the action bar with the given title and accent color. If title is null, then
    * the app logo will be shown instead of a title. Otherwise, a home button and title are
    * visible. If color is null, then the default colorstrip is visible.
    */
   public void setupActionBar(CharSequence title, int color) {
       final ViewGroup actionBarCompat = getActionBarCompat();
       if (actionBarCompat == null) {
           return;
       }

       LinearLayout.LayoutParams springLayoutParams = new LinearLayout.LayoutParams(0,
               ViewGroup.LayoutParams.FILL_PARENT);
       springLayoutParams.weight = 1;

       View.OnClickListener homeClickListener = new View.OnClickListener() {
           public void onClick(View view) {
               goHome();
           }
       };

       if (title != null) {
           // Add Home button
           addActionButtonCompat(R.drawable.ic_title_home, R.string.description_home,
                   homeClickListener, true);

           // Add title text
           TextView titleText = new TextView(mActivity, null, R.attr.actionbarCompatTextStyle);
           titleText.setLayoutParams(springLayoutParams);
           titleText.setText(title);
           actionBarCompat.addView(titleText);

       } else {
           // Add logo
           ImageButton logo = new ImageButton(mActivity, null, R.attr.actionbarCompatLogoStyle);
           logo.setOnClickListener(homeClickListener);
           actionBarCompat.addView(logo);

           // Add spring (dummy view to align future children to the right)
           View spring = new View(mActivity);
           spring.setLayoutParams(springLayoutParams);
           actionBarCompat.addView(spring);
       }

       setActionBarColor(color);
   }
   
   /**
    * Sets the action bar title to the given string.
    */
   public void setActionBarTitle(CharSequence title) {
       ViewGroup actionBar = getActionBarCompat();
       if (actionBar == null) {
           return;
       }

       TextView titleText = (TextView) actionBar.findViewById(R.id.actionbar_compat_text);
       if (titleText != null) {
           titleText.setText(title);
       }
   }
   
   public void setActionBarColor(int color) {
       if (color == 0) {
           return;
       }

       final View colorstrip = mActivity.findViewById(R.id.colorstrip);
       if (colorstrip == null) {
           return;
       }

       colorstrip.setBackgroundColor(color);
   }
   
}
